<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>微信JS-SDK Demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
    <link rel="stylesheet" href="http://demo.open.weixin.qq.com/jssdk/css/style.css?ts=1420774989">

    <!-- head 中 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">

    <!-- body 最后 -->
    <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.min.js"></script>
    <script src="/res/vue.js"></script>


</head>
<body ontouchstart="">
<div class="wxapi_container" id="app">


    <div class="weui-cells weui-cells_form">

        <div class="weui-cell">

            <div class="weui-cell__bd">
                <div class="weui-uploader">
                    <div class="weui-uploader__hd">
                        <p class="weui-uploader__title">图片上传</p>
                        <div class="weui-uploader__info">0/2</div>
                    </div>
                    <div class="weui-uploader__bd">
                        <ul class="weui-uploader__files" id="uploaderFiles">


                            <li class="weui-uploader__file" v-for="(item,index) in items" :data_id="item.id">
                                <img :src="item.url" style="width: 100%;height: 100%">
                            </li>


                        </ul>
                        <div class="weui-uploader__input-box">
                            <input id="uploaderInput" class="weui-uploader__input" type="button">
                        </div>
                    </div>
                </div>
            </div>


        </div>
        <div class="weui-cell">

            <div class="weui-cell__bd">
                <span class="desc">预览图片接口</span>
                <button class="btn btn_primary" id="previewImage2">预览</button>
            </div>


        </div>

    </div>

</div>
</body>
<!--<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script>-->
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>

    //vue对象
    var app = new Vue({
        el: '#app',
        data: {
            items: [
                {
                    url: 'http://img.chuaii.com/20180405/d515466e1f2f42e9be2af6f64fd78b60.jpg',
                    src: '10.jpg'
                }
            ]
        }
    });


    wx.config({
        debug: false,
        appId: '{$jsconfig.appId}',
        timestamp: '{$jsconfig.timestamp}',
        nonceStr: '{$jsconfig.noncestr}',
        signature: '{$jsconfig.signature}',
        jsApiList: [
            'checkJsApi',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'hideMenuItems',
            'showMenuItems',
            'hideAllNonBaseMenuItem',
            'showAllNonBaseMenuItem',
            'translateVoice',
            'startRecord',
            'stopRecord',
            'onRecordEnd',
            'playVoice',
            'pauseVoice',
            'stopVoice',
            'uploadVoice',
            'downloadVoice',
            'chooseImage',
            'previewImage',
            'uploadImage',
            'downloadImage',
            'getNetworkType',
            'openLocation',
            'getLocation',
            'hideOptionMenu',
            'showOptionMenu',
            'closeWindow',
            'scanQRCode',
            'chooseWXPay',
            'openProductSpecificView',
            'addCard',
            'chooseCard',
            'openCard'
        ]
    });
    wx.isInit = false;

    wx.ready(function () {



        // 5 图片接口
        // 5.1 拍照、本地选图
        var images = {
            localId: [],
            serverId: []
        };



        $('#previewImage2').click(function () {
            var urls=[];
            for(var index in app.items){
                urls.push(app.items[index].url);
            }
            wx.previewImage({
                current: 'http://img.chuaii.com/10.jpg',
                urls:urls
//                urls: [
//                    'http://img.hb.aicdn.com/b41cb301246b01ed9621978f51ec5fa121da8c912adab2-B4jrLN_fw658',
//                    'http://img.hb.aicdn.com/e18760df29548a98f6804ede7cb2a39a50f649c32ae619-TaZO7w_fw658',
//                    'http://img.hb.aicdn.com/766d3b0e13f87d144dc95ab503bc23dcad37d5632f209e-YROqMD_fw658'
//                ]
            });
        });

        $('#uploaderInput').click(function () {
            wx.chooseImage({
                success: function (res) {
                    images.localId = res.localIds;
                    console.log('已选择 ' + res.localIds.length + ' 张图片');
                    var i = 0, length = images.localId.length;
                    images.serverId = [];
                    function upload() {
                        wx.uploadImage({
                            localId: images.localId[i],
                            success: function (res) {

                                i++;
                                console.log('已上传：' + i + '/' + length);
                                //单个上传完成时
//                                record.send_img(res.serverId);

                                $.get('/index/api/tempfile', {mid: res.serverId}, function (res) {
                                    console.log(res);
                                    if(res.code==1){
                                        app.items.push(res.data);
                                    }
                                });

                                images.serverId.push(res.serverId);
                                if (i < length) {
                                    upload();
                                } else {
                                    //全部上传完成时


                                }
                            },
                            fail: function (res) {
                                alert(JSON.stringify(res));
                            }
                        });
                    }

                    upload();

                }
            });
        });






    });

    wx.error(function (res) {
        alert(res.errMsg);
    });


</script>

</html>